#This script executes regressions to provide difference-in-means summary statistics for Table A1. 

rm(list=ls())
gc()
library(reldist)
library(ineq)
library(stargazer)
library(data.table)
library(dplyr)
library(dtplyr)
library(ggplot2)
library(stringdist)
library(lfe)
library(haven)
library(readr)
library(broom)
library(tidylog)
library(rdrobust)
library(fixest)

# Set the working directory
setwd("/projects/opp_env/caa1970/")

# Source external R scripts for custom functions
source("Code/Child_Outcomes/child_outcomes_functions.R")
source("Code/rounding.r")

# Load the first-generation analysis dataset
load("Data/first_gen_analysis_data_jepmicro_acs.rda")

varlist <- c("parent_female", "white", "black", "hispanic", "other", "all_fullterm", "prec_fullterm", "tmax_fullterm", "county_pop", "PI_PC")


Table_A1 <- data.frame()
row_num<-1
for(i in varlist){
TabA1_1 <- feols(formula(paste0(i, "~nonattainment_CAA70")), data=parent_piks_acs %>% filter(year%in%1969:1971, !is.na(all_fullterm), !is.na(parent_female), !is.na(PI_PC)))

summary(TabA1_1, cluster=~county)
TabA1_1$nobs
temp_n <- rounding_rules_counts(TabA1_1$nobs)
temp_results <- tidy(TabA1_1, cluster=~county) %>% #tidy regression, keep coef of interest
  mutate_each(funs(signif(.,4)), -term) %>% #round appropriately
  mutate(N=temp_n, table = "A1", Row =row_num ) #add variables for N and control mean, plus index the table number and column
Table_A1 <- bind_rows(Table_A1, temp_results) #Appended to big dataset
row_num<-row_num+1
}
Table_A1

write_csv(Table_A1, "/projects/opp_env/caa1970/JPE_Micro/Output/Table A1/Table_A1.csv")